<?php
/**
 *
 *@package 
 *@copyright Copyright (c) 2012-2014 Tuniu.com
 *@author huxiaomin 2012
 *@version 1.0
 */
require_once TFRAME_DIR.'memcache/TMemcache.cls.php';
require_once TFRAME_DIR.'TImport.cls.php';

class TAutoload{
	//缓存文件夹
	static $folders = array();
	//缓存import的文件夹
	static $imports = array();

    static function load($class_name){
    	$dir = TFRAME_DIR;
    	if ($class_name == 'DDAO' || $class_name == 'DDb'){
    	   require_once $dir.'db/DDb.cls.php';
    	   return true;
    	}
    	if (file_exists($dir.$class_name.TFRAME_SUFFIX)){
    	   require_once $dir.$class_name.TFRAME_SUFFIX;
    	   return true;
    	}
    	else {
    	   $dirs_arr = self::getFolder($dir);
    	   foreach ($dirs_arr as $v){
    	       if (file_exists($v.'/'.$class_name.TFRAME_SUFFIX)){
    	           require_once $v.'/'.$class_name.TFRAME_SUFFIX;
    	           return true;
    	       }
    	   }
    	}
    	$imports = TImport::$imports;
    	foreach ((array)$imports as $import){
    	   $loadImport = self::loadImport($class_name,$import);
    	   if ($loadImport){
    	       return true;
    	   }
    	}
    	return false;
    	
    }
    
    /**
     * 返回文件夹 
     * @param string $dir
     * @return array
     */
    static function getFolder($dir){
        if (self::$folders){
            return self::$folders;
        }
        
    	$TMemcache = new TMemcache();
    	$gCacheKey = gCacheKey($dir);
    	$cache_value = $TMemcache->get($gCacheKey);
    	if ($cache_value){
    	   self::$folders = $cache_value;
    	   return $cache_value;
    	}
        
    	$res = opendir($dir);
        $return = array();
        if (!$res){
        	TLog::logDb("opendir fail:{$dir} when getFolder ",
                                        basename(__FILE__),__CLASS__.'.'.__FUNCTION__,'sys');
            return $return;
        }   
        
        while ($f = readdir($res)){
            if (is_dir($dir.$f) && $f{0} !== '.'){
                $return[] = $dir.$f;
            }
        }
        self::$folders = $return;
        $TMemcache->set($gCacheKey,$return,0,3600*24);
        return $return;
    }
    
    static function loadImport($class_name,$import){
        $dir = $import[0];
        $TFRAME_SUFFIX = $import[1];
        if (file_exists($dir.$class_name.$TFRAME_SUFFIX)){
           require_once $dir.$class_name.$TFRAME_SUFFIX;
           return true;
        }
        else {
           $dirs_arr = self::getImportFolder($import);
           foreach ($dirs_arr as $v){
               if (file_exists($v.'/'.$class_name.$TFRAME_SUFFIX)){
                   require_once $v.'/'.$class_name.$TFRAME_SUFFIX;
                   return true;
               }
           }
        }
        return false;
        
    }
    
    /**
     * 返回文件夹 
     * @param string $dir
     * @return array
     */
    static function getImportFolder($import){
        $dir = $import[0];
    	if (self::$imports[$dir]){
            return self::$imports[$dir];
        }
        
        $TMemcache = new TMemcache();
        $gCacheKey = gCacheKey($dir);
        $cache_value = $TMemcache->get($gCacheKey);
        if ($cache_value){
           self::$imports[$dir] = $cache_value;
           return $cache_value;
        }
        $res = opendir($dir);
        $return = array();
        if (!$res){
        	TLog::logDb("when ImportFolder ,openidr fail :{$dir} detail:".json_encode($import),
                                        basename(__FILE__),__CLASS__.'.'.__FUNCTION__,'sys');
            return $return;
        }   
        
        while ($f = readdir($res)){
            if (is_dir($dir.$f) && $f{0} !== '.'){
                $return[] = $dir.$f;
            }
        }
        self::$folders = $return;
        $TMemcache->set($gCacheKey,$return,0,3600*24);
        return $return;
    }
    
    
}